﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Excel;
namespace BYManageInfoSystem
{
    public partial class Frm_BGDOutGoodsRegSheet : DockContent
    {
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();
        UltraGridColumn ultraGridColumn1 = null;

        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        public bool IsNull(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public Frm_BGDOutGoodsRegSheet()
        {
            InitializeComponent();
        }

        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;
        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }

        private void InitCombox()
        {
            DataSet ds = m_bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxComp.DataSource = ds.Tables[0];
                this.comboBoxComp.DisplayMember = "经营单位简称";
                this.comboBoxComp.ValueMember = "BYCompanyTabID";
                this.comboBoxComp.SelectedIndex = 0;
            }
        }

        private void Frm_BGDOutGoodsRegSheet_Load(object sender, EventArgs e)
        {
            InitCombox();
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
        
        }

        private void buttonExport2Excel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }
        void ConvertStringToMultiLineFirst()
        {
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "集装箱柜号")
                {
                    int nRowsCount = this.ultraGrid1.Rows.Count;
                    for (int i = 0; i < nRowsCount; i++)
                    {
                        string strSource = this.ultraGrid1.Rows[i].GetCellValue(column2).ToString();
                        ConvertStringToMultiLineSecond(strSource, i);
                    }
                }
            }
        }

        private void ConvertStringToMultiLineSecond(string strSource, int nRowIndex)
        {
            string str = strSource.ToString();
            string[] arr = str.Split(',');
            string tempa = "";
            int n = 0;
            foreach (string ch in arr)
            {

                if (ch != "")
                {
                    if (n == 0)
                        tempa += ch;
                    else
                        tempa += "\n" + ch;
                }

                n++;
            }

            this.ultraGrid1.Rows[nRowIndex].Cells["集装箱柜号"].Value = tempa;
        }

        private void ProcessMultiHeader()
        {
            int nSpanX = 0;
            int nOriginX = 0;
            int nOriginXTemp = 0;
            bool bSpan = false;
            foreach (UltraGridColumn column2 in this.ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "工厂名称")
                    bSpan = false;

                if (!bSpan)
                {
                    if (str == "报关单金额")
                        continue;
                    column2.RowLayoutColumnInfo.SpanY = 4;
                    if (column2.Hidden == false)
                        nOriginXTemp += 2;
                }
                else
                {
                    nSpanX += 2;
                    column2.RowLayoutColumnInfo.OriginY = 2;
                }
                if (str == "PaidanRegisterTabID")
                {
                    bSpan = true;
                    nOriginX = nOriginXTemp;
                }
            }

            ultraGridColumn1 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("报关单金额", "报关单金额");
            ultraGridColumn1.CellActivation = Activation.Disabled;
            ultraGridColumn1.SortIndicator = SortIndicator.Disabled;
            ultraGridColumn1.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            ultraGridColumn1.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            ultraGridColumn1.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            ultraGridColumn1.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            ultraGridColumn1.RowLayoutColumnInfo.OriginY = 0;
            ultraGridColumn1.RowLayoutColumnInfo.SpanX = nSpanX;//日元跟人民币不要
            ultraGridColumn1.RowLayoutColumnInfo.SpanY = 2;
        }

        private void HideUnusedColumn()
        {
            bool bHidenStart = false;
            foreach (UltraGridColumn column2 in this.ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "申报月份")
                    bHidenStart = true;

                if (bHidenStart && str != "报关单金额")
                {
                    column2.Hidden = true;
                }
            }
        }

        private void ShowAllRecord(string strWhere)
        {
            m_bllHxdNoTab.row2col();
            ultraGrid1.DataSource = null;
            DataSet ds = m_bllHxdNoTab.ShowBGDOutGoodsReg(strWhere);

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if (Frm_Main.m_bCompleteVer == false)
                {
                    /*
                    ds.Tables[0].Columns["美元折扣"].ColumnName = "美元出口金额";
                    ds.Tables[0].Columns["港币折扣"].ColumnName = "港币出口金额";
                    */

                }

                //DataTable dtTemp = ds.Tables[0];
                //DataTable dtNewTemp = ds.Tables[0].Clone();
                //if (this.comboBoxComp.Text != "")
                //{
                //    for (int i = 0; i < dtTemp.Rows.Count; i++)
                //    {
                //        string strComp = dtTemp.Rows[i]["公司名称"].ToString();
                //        if (strComp == this.comboBoxComp.Text)
                //        {

                //            dtNewTemp.Rows.Add(dtTemp.Rows[i].ItemArray);
                //        }
                //    }
                //}
                //else
                //{
                //    for (int i = 0; i < dtTemp.Rows.Count; i++)
                //    {
                //        dtNewTemp.Rows.Add(dtTemp.Rows[i].ItemArray);
                //    }
                //}


                ultraGrid1.DataSource = ds.Tables[0];

                /*
                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                */
               // ConvertStringToMultiLineFirst();

                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
               // ultraGrid1.DisplayLayout.Bands[0].Columns[2].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[5].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns[8].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns[10].Hidden = true;
                //ultraGrid1.DisplayLayout.Bands[0].Columns[ultraGrid1.DisplayLayout.Bands[0].Columns.Count - 1].Hidden = true;


                ultraGrid1.DisplayLayout.Bands[0].Columns["美元折扣"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["港币折扣"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["欧元折扣"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["新加元折扣"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].Hidden = true;           
                
                //ProcessMultiHeader();
                HideUnusedColumn();

                if (Frm_Main.m_bCompleteVer == false)
                {
                    /*
                    ultraGrid1.DisplayLayout.Bands[0].Columns["美元"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["港币"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["欧元折扣"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["新加元折扣"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["欧元"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["新加元"].Hidden = true;
                    */
                }
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (IsNull(this.errorProvider1, this.textBoxYear))
                return;
            if (IsNull(this.errorProvider1, this.textBoxMonth))
                return;
            string strWhere = "";
            string strCondition1 = "";
            if(this.comboBoxComp.Text != "")
                strCondition1 = "BYCompanyTab.CompanyNameJX = '" + this.comboBoxComp.Text + "'";
            string strCondition2 = this.textBoxYear.Text + this.textBoxMonth.Text.PadLeft(2, '0') + " LIKE CONVERT(varchar(6), " + "HxdNoTab.TongJDate" + ", 112) ";
            if (this.comboBoxComp.Text != "")
                strWhere = "(" + strCondition1 + " AND " + strCondition2 + ")";
            else
                strWhere = strCondition2;
            ShowAllRecord(strWhere);
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
            ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            Worksheet ws = e.CurrentWorksheet;
            int iRdex = e.CurrentRowIndex;
            int iCdex = e.CurrentColumnIndex;
            //e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].CellFormat.FillPatternForegroundColor = Color.FromArgb(0, 0, 0); 
            // string strColumnName = e.GridColumn;
            if (e.GridColumn.Key == "起始的核销单号" || e.GridColumn.Key == "终止的核销单号")
            {
                //m_ugr[iRdex-1].Cells[e.GridColumn.Key + "公式"].Value.ToString()
                string strNo = e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value.ToString();
                int nNo = Convert.ToInt32(strNo, 10);
                if (nNo < 100000000)
                    e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value = "0" + e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value;
            }
        }
    }
}